//
//  vnExpressFunPostParser.m
//  Doc Bao
//
//  Created by Vinh Huynh on 10/29/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "NewsFullPostParser.h"


@implementation NewsFullPostParser

@synthesize dataGetter;
@synthesize appDelegate;
@synthesize htmlNews;
@synthesize idNumberNews;
 
-(void) getDataFrom:(NSString *)url withIdNews:(NSNumber *)idNews withDelegate:(id)aDelegate{
	self.idNumberNews = [[NSNumber alloc] init];
	self.idNumberNews = idNews;
	NSURL *dataUrl = [[NSURL alloc] initWithString:url];
	NSURLRequest *request = [[NSURLRequest alloc] initWithURL:dataUrl];
	self.appDelegate = aDelegate;
	[NSURLConnection connectionWithRequest:request delegate:self];
}

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
	self.dataGetter = [[NSMutableData alloc] initWithLength:0];
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
	[self.dataGetter appendData:data];
	
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
	NSString *htmlString = [[[NSString alloc] initWithData:self.dataGetter encoding:NSUTF8StringEncoding] autorelease];
	NSArray *arrayHtmlString = [[[NSArray alloc] init] autorelease];
	switch ([idNumberNews intValue]) {
		case 1:{
			NSLog(@"VNExpress");
			arrayHtmlString = [htmlString componentsSeparatedByString:@"<div class=\"content\">"];
			if ([arrayHtmlString count]>1) {
				NSString *tempNewsString = [NSString stringWithFormat:@"%@",[arrayHtmlString objectAtIndex:1]];
				NSArray *newsArray = [NSArray arrayWithArray:[tempNewsString componentsSeparatedByString:@"<div class=\"tag-parent\">"]] ;
				if ([newsArray count]>1){
					htmlNews = [[NSString alloc] initWithFormat:@"%@", [newsArray objectAtIndex:0]];
				}
			}
			
		}
			break;
		case 2:{
			NSLog(@"Tuoitre");
			NSLog(@"HTML : %@", htmlString);
			arrayHtmlString = [htmlString componentsSeparatedByString:@"<!-- MAIN CONTENT -->"];
			if ([arrayHtmlString count] > 1) {
				NSString *tempNewsString = [NSString stringWithFormat:@"%@",[arrayHtmlString objectAtIndex:1]];
				NSArray *newsArray = [NSArray arrayWithArray:[tempNewsString componentsSeparatedByString:@"<!-- CAC TIN LIEN QUAN -->"]] ;
				if ([newsArray count]>1){
					htmlNews = [[NSString alloc] initWithFormat:@"%@", [newsArray objectAtIndex:0]];
				}
			}
			else {
				arrayHtmlString = [htmlString componentsSeparatedByString:@"<div id=\"divContent\">"];
				if ([arrayHtmlString count] > 1) {
					NSString *tempNewsString = [NSString stringWithFormat:@"%@",[arrayHtmlString objectAtIndex:1]];
					NSArray *newsArray = [NSArray arrayWithArray:[tempNewsString componentsSeparatedByString:@"</div>"]] ;
					if ([newsArray count]>1){
						htmlNews = [[NSString alloc] initWithFormat:@"%@", [newsArray objectAtIndex:0]];
					}
				}
			}

		}
			break;
		case 3:{
			NSLog(@"HTML: %@,", htmlString);
			arrayHtmlString = [htmlString componentsSeparatedByString:@"<div class=\"articleDetailBox\">"];
			if ([arrayHtmlString count]>1) {
				NSString *tempNewsString = [NSString stringWithFormat:@"%@",[arrayHtmlString objectAtIndex:1]];
				NSArray *newsArray = [NSArray arrayWithArray:[tempNewsString componentsSeparatedByString:@"<div class"]] ;
				if ([newsArray count]>1){
					htmlNews = [[NSString alloc] initWithFormat:@"%@", [newsArray objectAtIndex:0]];
				}
			}
			else {
				arrayHtmlString = [htmlString componentsSeparatedByString:@"<div class=\"home-article-body pdr10\">"];
				if ([arrayHtmlString count]>1) {
					NSString *tempNewsString = [NSString stringWithFormat:@"%@",[arrayHtmlString objectAtIndex:1]];
					NSArray *newsArray = [NSArray arrayWithArray:[tempNewsString componentsSeparatedByString:@"<div  id="]] ;
					if ([newsArray count]>1){
						htmlNews = [[NSString alloc] initWithFormat:@"%@", [newsArray objectAtIndex:0]];
					}
				}
				
			}

		}
			break;

		default:
			break;
	}
	[self.appDelegate didEndGetNews:htmlNews];
	
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
	NSLog(@"Connection fail");
}

- (void)dealloc {
	self.htmlNews = nil;
	self.dataGetter = nil;
	self.idNumberNews = nil;
    [super dealloc];
}


@end
